package cn.migu.music.hangupsms.service.inner;

import cn.migu.music.common.utils.LogUtils;
import cn.migu.music.hangupsms.common.entity.BlackUserInfo;
import cn.migu.music.hangupsms.repository.BlackUserInfoDbMapper;
import cn.migu.music.hangupsms.service.LocalIdService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class InnerProcessBlackUserService {
    @Autowired
    private BlackUserInfoDbMapper blackUserInfoDbMapper;
    @Autowired
    private LocalIdService        localIdService;

    /**
     * 回填localId字段
     */
    public void writeLocalId() {
        int limitSize = 100;
        String phoneNumber = "0";
        while (true) {
            List<BlackUserInfo> blackUserInfoList = blackUserInfoDbMapper.selectByAllByPhoneNumber(phoneNumber, limitSize);
            if (CollectionUtils.isNotEmpty(blackUserInfoList)) {
                for (BlackUserInfo blackUserInfo : blackUserInfoList) {
                    phoneNumber = blackUserInfo.getPhonenumber();
                    Integer localId = localIdService.getLocalIdForHangUpBlackUser(phoneNumber);
                    if (localId == null) {
                        // 归属省份不存在
                        LogUtils.error(log, "手机号省份不存在:%s", phoneNumber);
                        blackUserInfoDbMapper.deleteBlack(phoneNumber);
                        continue;
                    }
                    blackUserInfo.setLocalId(localId);
                    blackUserInfoDbMapper.update(blackUserInfo);
                }
            }

            if (CollectionUtils.isEmpty(blackUserInfoList) || blackUserInfoList.size() < limitSize) {
                break;
            }
        }
    }
}
